import { BundleManager } from "./BundleManager";
import EndingAnimation from "./EndingAnimation";
import GameManager, { SharkAnim } from "./GameManager";
import { End } from "./Gwx/Ending";
import SharkSayManager, { SharkSayType } from "./SharkSayManager";
import TouchMonitor, { Touch_ObjectType } from "./TouchMonitor";
import Tween_breathe from "./Tween/Tween_breathe";
import WTool, { TweenType } from "./WTool";

const { ccclass, property } = cc._decorator;
@ccclass
export default class GameTigger extends TouchMonitor {

    start(): void {
        switch (this.ID) {
            case 0: break;//苹果点击触发
            case 1: break;//哑铃点击触发
            case 2: break;//睡觉点击触发
            case 3: break;//关闭灯泡点击触发
            case 4: break;//垃圾桶点击触发
            case 5: break;//出门点击触发
            case 6: break;//回房点击触发
            case 7: break;//打开灯泡点击触发
            case 8: break;//门外的花

            case 100: break;//苹果(生成的)
        }
        super.start();
    }

    //触摸按下
    OnTouchDown(even) {
        if (GameManager.Instance.GamePause) return;
        super.OnTouchDown(even);

    }
    //触摸拖拽
    OnTouchMove(even) {
        if (GameManager.Instance.GamePause || this.InitPoint == null) return;
        super.OnTouchMove(even);
    }
    //触摸抬起
    OnTouchUp(even) {
        if (this.ID == 7) {//开灯
            GameManager.GetNode("blackroom").active = false;
            GameManager.GetUINode("开灯").active = false;
            GameManager.Instance.SetPropBoxActive(true);
            GameManager.Instance.OnExitblackroom();
        }
        if (this.InitPoint != null) this.Restoration();
        if (GameManager.Instance.GamePause || this.InitPoint == null) return;
        super.OnTouchUp(even);
        if (this.ID == 0) {//苹果
            if (GameManager.Instance.sharkData[0] == -1) {
                GameManager.Instance.SharkSay([174]);
                return;
            }

            if (GameManager.Instance.sharkData[3] == 0) {//没吊过胃口
                GameManager.Instance.sharkData[3] = 1;
                GameManager.Instance.Tantalize();
                if (GameManager.SharkHurted) {//伤害过小鲨鱼
                    GameManager.Instance.SharkSay([111, 112, 113]);
                }
                else {
                    GameManager.Instance.SharkSay([2, 3]);
                    GameManager.Instance.LoadPrefab("手指", (pre) => {
                        let finger: cc.Node = cc.instantiate(pre);
                        finger.setParent(GameManager.Instance.GameNode.getChildByName("room"));
                        let face = GameManager.Instance.SharkNode.getChildByName("脸");
                        this.scheduleOnce(() => {
                            cc.tween(finger)
                                .call(() => {
                                    finger.setPosition(-400, -100);
                                })
                                .to(0.5, { position: cc.v3(face.x + face.parent.x, face.y + face.parent.y) })
                                .union()
                                .repeat(3)
                                .call(() => {
                                    finger.active = false;
                                })
                                .start()
                        }, 1)
                    })
                }
            }

            //生成苹果物体
            GameManager.Instance.LoadPrefab("苹果", (event: cc.Prefab) => {
                let pre = cc.instantiate(event);
                pre.getComponent(GameTigger).TargetNodes[0] = GameManager.Instance.SharkNode.getChildByName("脸").getChildByName("嘴巴触发");
                pre.setParent(GameManager.Instance.GameNode.getChildByName("room"));
                pre.setPosition(cc.v3(-400, 600));
                WTool.Tween_By(pre, cc.v2(0, -700), 1.5, WTool.TweenTypeName[TweenType.弹跳效果的缓出函数]);
            });
        }
        if (this.ID == 1) {//哑铃
            GameManager.Instance.sharkData[1]++;
            let speed: number = 0;
            if (GameManager.Instance.sharkData[1] < 5) {
                speed = 0.5;
            }
            else {
                if (GameManager.Instance.sharkData[1] == 7) {
                    let yintang = GameManager.Instance.SharkNode.getChildByName("印堂发黑");
                    yintang.opacity = 0;
                    yintang.active = true;
                    cc.tween(yintang)
                        .to(3, { opacity: 255 })
                        .start();
                }
                speed = 1 - GameManager.Instance.sharkData[1] * 0.1;
                if (speed <= 0.1) speed = 0.1
            }
            GameManager.Instance.Anim.getAnimationState(SharkAnim.Anim_哑铃).speed = speed;
            GameManager.Instance.GamePause = true;
            GameManager.Instance.Anim.play(SharkAnim.Anim_哑铃);
            GameManager.Instance.Anim.once(cc.Animation.EventType.FINISHED, () => {
                if (GameManager.Instance.sharkData[1] == 1) {
                    GameManager.Instance.ChanggeSharkSize(0);//减肥
                }
                GameManager.Instance.GamePause = false;
                GameManager.Instance.SharkSay(SharkSayManager.GetSharkSayDate(SharkSayType.举哑铃));
                if (GameManager.Instance.sharkData[1] == 9) {
                    EndingAnimation.PlayEndingAnimation(5);
                }
                GameManager.Instance.sharkData[0] = -1;
                GameManager.GetUINode("道具框/睡觉").active = true;
                GameManager.GetUINode("道具框/出门").active = true;
            })
        }
        if (this.ID == 2) {//睡觉
            GameManager.Instance.sharkData[2]++;
            GameManager.Instance.sharkData[1] = 0;
            GameManager.Instance.SharkNode.getChildByName("印堂发黑").active = false;
            GameManager.Instance.GamePause = true;
            GameManager.Instance.Anim.play(SharkAnim.Anim_睡觉);
            GameManager.Instance.SharkNode.getChildByName("脸").active = false;
            GameManager.Instance.Anim.once(cc.Animation.EventType.FINISHED, () => {
                GameManager.Instance.GamePause = false;
                GameManager.Instance.SharkNode.getChildByName("脸").active = true;
                GameManager.Instance.SharkSay(SharkSayManager.GetSharkSayDate(SharkSayType.睡觉));
            })
            if (GameManager.Instance.sharkData[2] == 7) {
                this.ObjectType = Touch_ObjectType.拖拽到某位置放开触发;
                this.ID = 101;
                this.writeoff();
                this.start();
                this.TargetNodes[0] = GameManager.GetUINode("道具框/垃圾桶");
            }
        }

        if (this.ID == 3) {//关灯
            if (GameManager.Instance.sharkData[7] == 1) {//已经关灯过了
                GameManager.Instance.SharkSay([175]);
                return;
            }
            GameManager.GetNode("blackroom").active = true;
            GameManager.GetUINode("开灯").active = true;
            GameManager.Instance.SetPropBoxActive(false);
            GameManager.Instance.Onblackroom();
        }

        if (this.ID == 5) {//出门
            GameManager.GetNode("room").active = false;
            GameManager.GetUINode("回房").active = true;
            GameManager.Instance.SetPropBoxActive(false);
            GameManager.Instance.Onoutdoorincident();
        }
        if (this.ID == 6) {//回房
            if (GameManager.GetNode("outdoor/流浪鲨鱼").active) {
                GameManager.Instance.SharkSay([43], () => {
                    GameManager.Instance.OnoutdoorToroomincident();
                })
                return;
            }
            GameManager.Instance.OnoutdoorToroomincident();
        }

        if (this.ID == 8) {//门外的花
            this.writeoff();
            GameManager.Instance.GamePause = true;
            this.node.getComponent(Tween_breathe).TweenId.stop();

            WTool.Tween_By(this.node, cc.v2(0, 140), 0.5, "", () => {
                WTool.Tween_Opacity(this.node, 1, 255, 0);
                WTool.Tween_To(this.node, WTool.getWorldPos(GameManager.GetUINode("回房")), 1, "", () => {
                    GameManager.GetNode("room/花").active = true;
                    GameManager.Instance.GamePause = false;
                })
            })
        }
        if (this.ID == 9) {//流浪的鲨鱼
            this.writeoff();
            GameManager.GetUINode("回房").active = false;
            const GoHome = () => {
                this.node.getComponent(Tween_breathe).TweenId.stop();
                WTool.Tween_Opacity(this.node, 2, 255, 0);
                WTool.Tween_To(this.node, WTool.getWorldPos(GameManager.GetUINode("回房")), 2, "", () => {
                    this.node.active = false;
                    GameManager.GetUINode("回房").active = true;
                    GameManager.Instance.cb = () => {
                        GameManager.Instance.GamePause = true;
                        if (GameManager.Instance.SharkNode.active) {
                            GameManager.Instance.SharkNode.setPosition(-130, GameManager.Instance.SharkNode.y);
                            GameManager.Instance.SharkNode.getComponent(Tween_breathe).TweenId.stop();
                            GameManager.Instance.SharkNode.setScale(-1.2, 1.2);
                            GameManager.Instance.SharkNode.getComponent(Tween_breathe).turnRightTween(1.2);
                            let liulang = GameManager.GetNode("room/流浪鲨鱼");
                            liulang.active = true;
                            this.scheduleOnce(() => { liulang.getComponent(Tween_breathe).TweenId.stop(); })
                            liulang.setScale(1.2, 1.2);
                            WTool.Tween_to(liulang, cc.v2(130, liulang.y), 2, "", () => {
                                liulang.getComponent(Tween_breathe).resetTween(1.2);
                                GameManager.Instance.SharkSay([97, 98], () => {
                                    EndingAnimation.PlayEndingAnimation(7);
                                })
                            })
                        }
                        else {
                            let liulang = GameManager.GetNode("room/流浪鲨鱼");
                            liulang.active = true;
                            this.scheduleOnce(() => { liulang.getComponent(Tween_breathe).TweenId.stop(); })
                            liulang.setScale(1.2, 1.2);
                            GameManager.GetUINode("道具框/出门").active = false;
                            WTool.Tween_to(liulang, cc.v2(0, liulang.y), 2, "", () => {
                                liulang.getComponent(Tween_breathe).resetTween(1.2);
                                GameManager.Instance.SharkSay([98, 99, 100, 101, 102, 103, 104, 105, 106, 107], () => {
                                    GameManager.Instance.LoadPrefab("SelectDlg", (pre) => {
                                        cc.instantiate(pre).setParent(GameManager.Instance.UINode);
                                    })
                                })
                            })
                        }
                    }
                })
            }
            if (GameManager.Instance.SharkNode.active) {
                GoHome();
            }
            else {
                GameManager.Instance.SharkSay([96], () => {
                    GoHome();
                })
            }
        }

        if (this.ID == 10) {//返回主页
            this.writeoff();
            GameManager.SharkHurted = false;
            GameManager.EndData = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];//还原结局
            cc.director.loadScene("MainPanel");
        }
    }

    //事件接口,拖拽到某位置放开触发_触发事件
    TouchMoveInCident(TargetNode: cc.Node) {
        if (this.ID == 100) {//苹果
            if (GameManager.Instance.sharkData[0] == -1) {
                this.Restoration();
                return;
            }
            BundleManager.GetBundle("AudioClips").load("吃苹果", cc.AudioClip, (err, event) => {
                cc.audioEngine.playEffect(event, false);
            })
            GameManager.Instance.ChanggeSharkSize(GameManager.Instance.sharkData[0] + 1);
            GameManager.Instance.SharkSay(SharkSayManager.GetSharkSayDate(SharkSayType.吃苹果));
            if (GameManager.Instance.sharkData[0] == 1) {//吃到苹果了   
                GameManager.GetUINode("道具框/哑铃").active = true;
                GameManager.GetUINode("道具框/垃圾桶").active = true;
                if (GameManager.Instance.GetEndingIsFinish(End.结局1) == 1) {
                    GameManager.GetNode("room/胖胖鲨按钮").active = true;
                }
            }
            if (GameManager.Instance.sharkData[4] == 0) {//吊胃口是否被中断
                GameManager.Instance.sharkData[4] = 1;
            }
            this.node.destroy();
        }
        if (this.ID == 101) {//睡觉
            WTool.Tween_To(this.node, WTool.getWorldPos(GameManager.GetUINode("道具框/垃圾桶")).add(cc.v2(0, 100)), 0.8, "", () => {
                WTool.Tween_Scale(this.node, 1, 1, 0, () => {
                    GameManager.Instance.SharkSay([93, 94, 95]);
                    EndingAnimation.PlayEndingAnimation(6);
                    this.node.destroy();
                })
                WTool.Tween_By(this.node, cc.v2(0, -100), 1)
            })
        }
        if (this.ID == 102) {//室内的花
            GameManager.Instance.sharkData[5] = 1;
            GameManager.Instance.SharkNode.getChildByName("头花").active = true;
            GameManager.Instance.SharkSay([68, 69, 70, 71, 72, 73]);
            this.node.destroy();
        }
    }

}
